package com.xiaoyu.lanling.widget.momentgrid;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.view.SimpleDraweeView;
import com.xiaoyu.base.app.App;
import com.xiaoyu.base.view.list.SafeGridLayoutManager;
import com.xiaoyu.lanling.feature.moment.model.MomentItem;
import com.xiaoyu.lanling.media.image.ImageViewUtil;
import com.xiaoyu.lanling.router.Router;
import com.xiaoyu.lanling.util.ViewTagUtil;

import java.util.List;

import in.srain.cube.util.LocalDisplay;
import in.srain.cube.views.list.CubeRecyclerViewSimpleAdapter;

public class MomentGridLayoutView extends FrameLayout {

    public static final int GRID_SIZE_WITH_PX_FOR_ONE = LocalDisplay.dp2px(200);
    // NOTE take avatar size into account , the avatar and margin sum to 80dp
    public static final int GRID_WIDTH_WITH_PX_FOR_MORE = LocalDisplay.SCREEN_WIDTH_PIXELS - LocalDisplay.dp2px(80 + 32);

    public static final int GRID_ITEM_SIZE_WITH_PX_FOR_ONE = GRID_SIZE_WITH_PX_FOR_ONE;
    public static final int GRID_ITEM_SIZE_WITH_PX_FOR_ONE_ROW_FOR_TWO = (GRID_WIDTH_WITH_PX_FOR_MORE - LocalDisplay.dp2px(2 + 2)) / 2;
    public static final int GRID_ITEM_SIZE_WITH_PX_FOR_ONE_ROW = (GRID_WIDTH_WITH_PX_FOR_MORE - LocalDisplay.dp2px(2 * 2 + 2)) / 3;
    public static final int GRID_ITEM_SIZE_WITH_PX_FOR_TWO_ROWS = GRID_ITEM_SIZE_WITH_PX_FOR_ONE_ROW * 2 + LocalDisplay.dp2px(2);
    public static final int GRID_ITEM_SIZE_WITH_PX_FOR_THREE_ROWS = GRID_ITEM_SIZE_WITH_PX_FOR_ONE_ROW * 3 + LocalDisplay.dp2px(2 * 2);

    private final OnClickListener sOnSingleImageClickListener = v -> {
        final MomentGridItem item = ViewTagUtil.getTag(v, MomentGridItem.class);
        final Context context = App.getInstance().getTopActivity();
        if (item != null && context != null) {
            Router.Companion.getInstance().gotoMomentPhotoActivity(context, item.imageUrl, item.imageUrlList, item.feedId);
        }
    };

    private SimpleDraweeView mSingleImageView;
    private RecyclerView mMultiImageViews;
    private CubeRecyclerViewSimpleAdapter<MomentGridItem> mMomentGridAdapter;

    public MomentGridLayoutView(@NonNull Context context) {
        this(context, null);
    }

    public MomentGridLayoutView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MomentGridLayoutView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initBind();
    }

    private void initView() {
        mSingleImageView = new SimpleDraweeView(getContext());
        mSingleImageView.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP);

        mMultiImageViews = new RecyclerView(getContext());
        mMomentGridAdapter = new CubeRecyclerViewSimpleAdapter<>();
        mMomentGridAdapter.setViewHolderClass(0, null, MomentGridViewHolder.class, 9);
        mMultiImageViews.setAdapter(mMomentGridAdapter);
        mMultiImageViews.setNestedScrollingEnabled(false);

        addView(mSingleImageView, new LayoutParams(GRID_SIZE_WITH_PX_FOR_ONE,
                GRID_SIZE_WITH_PX_FOR_ONE));
        addView(mMultiImageViews);
    }

    private void initBind() {
        mSingleImageView.setOnClickListener(sOnSingleImageClickListener);
    }

    private void setGridNum(int gridNum) {
        if (gridNum == 1) {
            mMultiImageViews.setVisibility(GONE);
            mSingleImageView.setVisibility(VISIBLE);
        } else {
            mMultiImageViews.setVisibility(VISIBLE);
            mSingleImageView.setVisibility(GONE);
            mMultiImageViews.setTag(getTag());

            switch (gridNum) {
                case 2:
                    mMultiImageViews.getLayoutParams().width = GRID_WIDTH_WITH_PX_FOR_MORE;
                    mMultiImageViews.getLayoutParams().height = GRID_ITEM_SIZE_WITH_PX_FOR_ONE_ROW_FOR_TWO;
                    break;
                case 3:
                    mMultiImageViews.getLayoutParams().width = GRID_WIDTH_WITH_PX_FOR_MORE;
                    mMultiImageViews.getLayoutParams().height = GRID_ITEM_SIZE_WITH_PX_FOR_ONE_ROW;
                    break;
                case 4:
                    mMultiImageViews.getLayoutParams().width = GRID_WIDTH_WITH_PX_FOR_MORE - LocalDisplay.dp2px(2) - GRID_ITEM_SIZE_WITH_PX_FOR_ONE_ROW;
                    mMultiImageViews.getLayoutParams().height = GRID_ITEM_SIZE_WITH_PX_FOR_TWO_ROWS;
                    break;
                case 5:
                case 6:
                    mMultiImageViews.getLayoutParams().width = GRID_WIDTH_WITH_PX_FOR_MORE;
                    mMultiImageViews.getLayoutParams().height = GRID_ITEM_SIZE_WITH_PX_FOR_TWO_ROWS;
                    break;
                case 7:
                case 8:
                case 9:
                    mMultiImageViews.getLayoutParams().width = GRID_WIDTH_WITH_PX_FOR_MORE;
                    mMultiImageViews.getLayoutParams().height = GRID_ITEM_SIZE_WITH_PX_FOR_THREE_ROWS;
                    break;
                default:
                    setVisibility(GONE);
                    return;
            }

            SafeGridLayoutManager layoutManager = new SafeGridLayoutManager(getContext(), (gridNum == 2 || gridNum == 4) ? 2 : 3);
            mMultiImageViews.setLayoutManager(layoutManager);
        }
    }

    private void setData(List<MomentGridItem> momentGridItems) {
        if (momentGridItems == null || momentGridItems.size() <= 0) {
            setVisibility(GONE);
            return;
        }
        setVisibility(VISIBLE);

        if (momentGridItems.size() == 1) {
            MomentGridItem item = momentGridItems.get(0);
            LayoutParams layoutParams = new LayoutParams(item.itemWidth, item.itemHeight);
            mSingleImageView.setLayoutParams(layoutParams);
            ImageViewUtil.INSTANCE.loadImage(mSingleImageView, item.mImageLoadParam);
            ViewTagUtil.setViewTagForClick(mSingleImageView, item);
        } else {
            mMomentGridAdapter.setList(momentGridItems);
            mMomentGridAdapter.notifyDataSetChanged();
        }
    }

    public void showData(MomentItem momentItem) {
        List<MomentGridItem> momentGridItems = momentItem.getFeed().getImages();
        for (MomentGridItem item : momentGridItems) {
            item.feedId = momentItem.getFeed().getId();
        }
        setGridNum(momentGridItems.size());
        setData(momentGridItems);
    }
}
